package com.qingge.springboot.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingge.springboot.entity.Type;
import com.qingge.springboot.entity.dto.FlowerDto;
import com.qingge.springboot.mapper.FlowerMapper;
import com.qingge.springboot.mapper.TypeMapper;
import com.qingge.springboot.utils.BeanUtilsPlus;
import com.qingge.springboot.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingge.springboot.common.Result;

import com.qingge.springboot.service.IFlowerService;
import com.qingge.springboot.entity.Flower;

import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/flower")
public class FlowerController {

    @Resource
    private IFlowerService flowerService;

    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private FlowerMapper flowerMapper;

    // 新增或者更新
    @Transactional
    @PostMapping
    public Result save(@RequestBody Flower flower) {
        if (flower.getFid() != null) {
            QueryWrapper<Flower> wrapper = new QueryWrapper<>();
            wrapper.ne("fid", flower.getFid()).
                    eq("fname", flower.getFname());
            if (flowerService.getOne(wrapper) != null) {
                return Result.error("该鲜花已存在!");
            }
        // 新增花，则对应类型数量+1
        } else {
            Type type = typeMapper.selectById(flower.getTid());
            type.setNum(type.getNum()+1);
            typeMapper.updateById(type);
        }
        flowerService.saveOrUpdate(flower);
        return Result.success();
    }

    @PostMapping("/photo")
    public Result uploadPhoto(@RequestBody Flower flower) {
        flowerService.saveOrUpdate(flower);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        Flower flower = flowerService.getById(id);
        Type type = typeMapper.selectById(flower.getTid());
        type.setNum(type.getNum()-1);
        typeMapper.updateById(type);
        flowerService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        QueryWrapper<Flower> wrapper = new QueryWrapper<>();
        wrapper.in("fid", ids);
        List<Flower> flowers = flowerMapper.selectList(wrapper);
        for (Flower flower : flowers) {
            Type type = typeMapper.selectById(flower.getTid());
            type.setNum(type.getNum()-1);
            typeMapper.updateById(type);
        }
        flowerService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping()
    public Result findAll() {
        return Result.success(flowerService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(flowerService.getById(id));
    }



    @GetMapping("/page")
    public Result findPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                           @RequestParam(required = false) String fname,
                           @RequestParam(required = false) String obj
    ) {
        QueryWrapper<Flower> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("fid").
                like(!StringUtil.isEmptyOrNull(fname),"fname", fname).
                like(!StringUtil.isEmptyOrNull(obj),"obj", obj);
        Page<Flower> page = flowerMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        List<Flower> records = page.getRecords();
        List<FlowerDto> dtoList = BeanUtilsPlus.batchCopyProperties(records, FlowerDto.class);
        for (FlowerDto dto : dtoList) {
            dto.setTname(typeMapper.selectById(dto.getTid()).getTname());
        }
        JSONObject flowerData = new JSONObject();
        flowerData.put("records", dtoList);
        flowerData.put("total", page.getTotal());
        flowerData.put("size", page.getSize());
        flowerData.put("current", page.getCurrent());
        flowerData.put("pages", page.getPages());
        return Result.success(flowerData);
    }

}

